#include "keywords_panel.h"
#include "app_frame.h"
#include "util.h"

KeywordsPanel::KeywordsPanel(AppFrame *top_frame, wxWindow* parent)
    : NotesPanelBase(top_frame, parent)
{
    dbgenter("KeywordsPanel::KeywordsPanel()\n")
    this->top_frame = top_frame;
    render_options.collapse = true;
}

void KeywordsPanel::SetFilter(const KeywordFilter& filter)
{
    this->filter = filter;

    items.clear();

    // matching keywords
    int cnt = keyword_list.KeywordCount();
    for (int i=0; i<cnt; i++) {
        Keyword k = keyword_list.KeywordByIndex(i);
        if (filter.Matches(k))
            items.push_back(TextItem(k));
    }

    // matching aliases
    if (!filter.name.IsEmpty()) {
        vector<Keyword> alias_keywords;
        keyword_list.KeywordsByAlias(alias_keywords, filter.name);
        for (unsigned i=0; i<alias_keywords.size(); i++) {
            items.push_back(TextItem(wxFormat(_("{{%s}} could be an alias of {{%s}}"), filter.name, alias_keywords[i].name)));
        }
    }

    UpdateDisplay();
}

void KeywordsPanel::NotifyNewNote(const Note& n)
{
    dbgenter("KeywordsPanel::NotifyNewNote(%s)\n", n.ToString());
}


void KeywordsPanel::NotifyEditedNote(const Note& n)
{
    dbgenter("KeywordsPanel::NotifyEditedNote(%s)\n", n.ToString());
}

void KeywordsPanel::NotifyDeletedNote(const Note& n)
{
    dbgenter("KeywordsPanel::NotifyDeletedNote(%s)\n", n.ToString());
}

void KeywordsPanel::NotifyNewKeyword(const Keyword& k)
{
    dbgenter("KeywordsPanel::NotifyNewKeyword(%s)\n", k.ToString());
    SetFilter(filter);
}

void KeywordsPanel::NotifyEditedKeyword(const Keyword& k)
{
    dbgenter("KeywordsPanel::NotifyEditedKeyword(%s)\n", k.ToString());
    SetFilter(filter);
}

void KeywordsPanel::NotifyDeletedKeyword(const Keyword& k)
{
    dbgenter("KeywordsPanel::NotifyDeletedKeyword(%s)\n", k.ToString());
    SetFilter(filter);
}
